<?php
declare(strict_types=1);

namespace catcher;

use think\Paginator;
use think\response\Json;

class CatchResponse
{
    /**
     * 成功的响应
     *
     * @time 2019年12月02日
     * @param array $data
     * @param string $errorMessage
     * @param int $errorCode
     * @param bool $success
     * @param int $showType
     * @return Json
     */
  public static function success($data = [], $errorMessage = 'success', $showType = 0, $errorCode = Code::SUCCESS , $success = true ): Json
  {
      return json([
          'code' => $errorCode,
          'message' => $errorMessage,
          'success' => $success,
          'errorCode' => $errorCode,
          'errorMessage'  => $errorMessage,
          'showType'=> $showType,
          'data'    => $data,
      ]);
  }

    /**
     * 分页
     *
     * @time 2019年12月06日
     * @param Paginator $list
     * @param bool $success
     * @param int $showType
     * @param string $errorMessage
     * @param string $traceId
     * @param string $host
     * @return mixed
     */
  public static function paginate(Paginator $list,$success = true,$showType = 0,$errorMessage = '',$traceId = '',$host = '')
  {
        return json([
          'code' => Code::SUCCESS,
          'message' => $errorMessage,
          'success' => $success,
          'errorCode'    => Code::SUCCESS,
          'errorMessage' => $errorMessage,
          'showType'=> $showType,
          'traceId'=>$traceId,
          'host'=>$host,
          'total'   => $list->total(),
          'current' => $list->currentPage(),
          'pageSize'   => $list->listRows(),
          'data'    => $list->getCollection(),
        ]);
  }

    /**
     * 错误的响应
     * 当 success = false 时提示信息
     *export enum ErrorShowType {
        SILENT = 0 不提示错误,
        WARN_MESSAGE = 1 警告信息提示,
        ERROR_MESSAGE = 2 错误信息提示,
        NOTIFICATION = 4 通知提示,
        REDIRECT = 9 页面跳转
       }
     * @time 2019年12月02日
     * @param string $errorMessage
     * @param int $errorCode
     * @param bool $success
     * @param int $showType
     * @return Json
     */
  public static function fail($errorMessage = '', $errorCode = Code::FAILED, $showType = 2 ,$success = false): Json
  {
        return json([
            'code' => $errorCode,
            'message' => $errorMessage,
            'success' => $success,
            'errorCode' => $errorCode,
            'errorMessage'  => $errorMessage,
            'showType'=> $showType,
        ]);
  }
}
